package ar.uba.fi.RSACryptoFiuba.model;

public abstract class RSAAlgorithmStrategy {
	
	public void movePreviousStep(RSAAlgorithm algorithm){
		if(algorithm.getCurrentStep().isFirst()){
			throw new NullPointerException();
		}
		algorithm.setCurrentStep(algorithm.getCurrentStep().getPrevious());
	}
	
	public void moveNextStep(RSAAlgorithm algorithm){
		if(algorithm.getCurrentStep().isLast()){
			throw new NullPointerException();
		}
		algorithm.setCurrentStep(algorithm.getCurrentStep().getNext());
	}

	public void reset(RSAAlgorithm algorithm) {
		this.undoStep(algorithm);
		while(!algorithm.getCurrentStep().isFirst()){
			this.movePreviousStep(algorithm);
			this.undoStep(algorithm);
		}
	}

	public void undoStep(RSAAlgorithm algorithm) {
		algorithm.getCurrentStep().undo(algorithm.getMessage());	
	}
	
	public void executeStep(RSAAlgorithm algorithm){
		this.executeStrategy(algorithm);
	}
	
	public abstract void executeStrategy(RSAAlgorithm algorithm);

	public void resolve(RSAAlgorithm algorithm){
		this.executeStep(algorithm);
		while(!algorithm.getCurrentStep().isLast()){
			this.moveNextStep(algorithm);
			this.executeStep(algorithm);
		}
	}

}
